package com.jiai.cloud.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.page.CustomPage;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.entity.TApplication;
import com.jiai.cloud.entity.TGoodType;
import com.jiai.cloud.feign.GoodTypeAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created with j360-cloud-all -> me.j360.cloud.eurekaclient.controller.
 * User: min_xu
 * Date: 2015/10/9
 * Time: 10:53
 * 说明：
 */

@RestController
@RequestMapping("/api/v1/goodtype")
public class GoodTypeController {


    @Autowired
    GoodTypeAPI goodtypeAPI;

    /**
     * 直接调用feign，feign会去调用eurekaService
     * */

    //增
    @RequestMapping(value="/addGoodType", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object saveGoodType(@RequestBody TGoodType tGoodType) throws ServiceException {
        try {
            goodtypeAPI.addGoodType(tGoodType);
            return RestResultGenerator.genSuccessResult("");
        }catch (Exception e){
            throw new ServiceException("插入失敗",e.getMessage());
        }
    }
    //删
    @RequestMapping(value="/delete/{id}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object deleteById(@PathVariable("id") Integer id) throws ServiceException {
        try {
            goodtypeAPI.delete(id);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗",e.getMessage());
        }
    }

<<<<<<< Updated upstream
    //通过ID查询
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public Object findById(@PathVariable("id") Integer id) throws ServiceException {
        try{
            goodtypeAPI.selectById(id);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }

    //查询所有的
    @RequestMapping(value="/findByAll/{page}",method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public Object findOreadDetails(@PathVariable("page") Integer page1) throws ServiceException {
        try {
            Page<TApplication> page = new Page<>(page1, 20);
            goodtypeAPI.selectPage(page1);
            return RestResultGenerator.genSuccessResult(new CustomPage(page));
        } catch (Exception e) {
            throw new ServiceException("chaxun", e.getMessage());
        }
    }
=======

>>>>>>> Stashed changes


    //改(有就改，没有就插入)
    @RequestMapping(value="/edit",method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object updateOrInsert(@RequestBody TGoodType tGoodType)throws ServiceException {
        try {
            goodtypeAPI.edit(tGoodType);
            return RestResultGenerator.genSuccessResult("");
        } catch (Exception e) {
            throw new ServiceException("编辑失敗", e.getMessage());
        }
    }


}
